abstract class Colleague {
    public abstract onEvent(eventType: string): void
}

class ConcreateColleagueA extends Colleague {
    private mediator: Mediator

    constructor(mediator: Mediator) {
        super()
        this.mediator = mediator
    }

    public onEvent(eventType: string): void {
        this.mediator.doEvent(eventType)
    }

    public doSomething(): void {
        console.log("ConcreateColleagueA is working");
    }
}

class ConcreateColleagueB extends Colleague {
    private mediator: Mediator

    constructor(mediator: Mediator) {
        super()
        this.mediator = mediator
    }

    public onEvent(eventType: string): void {
        this.mediator.doEvent(eventType)
    }

    public doSomething(): void {
        console.log("ConcreateColleagueB is working");
    }
}


abstract class Mediator {
    protected _colleagueA?: ConcreateColleagueA | undefined;
    public get colleagueA(): ConcreateColleagueA | undefined {
        return this._colleagueA;
    }
    public set colleagueA(value: ConcreateColleagueA | undefined) {
        this._colleagueA = value;
    }
    public _colleagueB?: ConcreateColleagueB | undefined;


    public get colleagueB(): ConcreateColleagueB | undefined {
        return this._colleagueB;
    }
    public set colleagueB(value: ConcreateColleagueB | undefined) {
        this._colleagueB = value;
    }

    public abstract doEvent(eventType: string): void
}

class ConcreateMediator extends Mediator {
    //根据收到的消息完成相应的任务，中介者起到协调各个方法的执行以完成任务的作用
    public doEvent(eventType: string): void {
        switch (eventType) {
            case 'A':
                this.doColleagueAEvent()
                break
            case 'B':
                this.doColleagueBEvent()
                break
            default: null
        }
    }

    public doColleagueAEvent(): void {
        super._colleagueA && super._colleagueA.doSomething()
        super._colleagueB && super._colleagueB.doSomething()
        console.log("A -> B 执行完成");
    }

    public doColleagueBEvent(): void {
        super._colleagueB && super._colleagueB.doSomething()
        super._colleagueA && super._colleagueA.doSomething()
        console.log("B -> A 执行完成")
    }
}

const MediatorTest = (): void => {
    const mediator: Mediator = new ConcreateMediator()
    //中介者与具体对象双向绑定，具体对象要做什么事情需要通知中介者，中介者调用该事件相关方法完成任务（如卖房子，买方、中介、卖方）
    const colleagueA: Colleague = new ConcreateColleagueA(mediator)
    const colleagueB: Colleague = new ConcreateColleagueB(mediator)
    mediator.colleagueA = <ConcreateColleagueA>colleagueA
    mediator.colleagueB = <ConcreateColleagueB>colleagueB

    colleagueA.onEvent('A')
    colleagueB.onEvent('B')
}

MediatorTest()